﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace smartvoid.secciones
{
    public partial class ComentariosDebate : System.Web.UI.Page
    {
        int idGrupo = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Grupo"] != null)
            {
                idGrupo = Int32.Parse(Session["Grupo"].ToString());
            }
            string idDebate = Request.QueryString["IdDebate"];
            PHComentariosDebate.Controls.Clear();
            Literal htmlCode = new Literal();
            htmlCode.Text = @"<table class=""hovertable"">
                                    <tr>
	                                <th style=""width: 110px"">Comentado Por</th>
                                    <th>Comentario</th>
                                    <th style=""width: 160px"">Fecha Comentario</th>
                                    </tr>";
            using (var context = new SmartVoidEntities())
            {
                var query = (from a in context.Comentarios
                             where a.c_IDGrupo == idGrupo
                             orderby a.c_IDComentario descending
                             select a);
                if (query != null)
                {
                    lblTituloDelDebate.Text = obtenerNombreDebate(query.FirstOrDefault().c_IDDebate);
                    foreach (var obj in query)
                    {
                        htmlCode.Text = htmlCode.Text + @"<tr onmouseover=""this.style.backgroundColor='#EDCEE8';"" onmouseout=""this.style.backgroundColor='#EAEAEA';"">";
                        htmlCode.Text = htmlCode.Text + "<td>" + obj.c_Autor + "</td>"; // Fecha de Creación
                        htmlCode.Text = htmlCode.Text + @"<td>" + obj.c_Mensaje + "</td>"; // Titulo Debate
                        htmlCode.Text = htmlCode.Text + "<td>" + obj.c_FechaDeCreacion + "</td>"; // Usuario Creador
                        htmlCode.Text = htmlCode.Text + "</tr>";
                    }
                }
            }
            htmlCode.Text = htmlCode.Text + "</table>";
            PHComentariosDebate.Controls.Add(htmlCode);
            //}
        }

        public string obtenerNombreDebate(int idDebate)
        {
            using (var context = new SmartVoidEntities())
            {
                var query = (from a in context.Debates
                             where a.d_IDDebate == idDebate
                             select a).FirstOrDefault();
                return query.d_Titulo.ToString();
            }
        }

        int contarComentarios()
        {
            using (var context = new SmartVoidEntities())
            {
                int contar;

                try
                {
                    contar = context.Comentarios.Max(p => p.c_IDComentario);
                }
                catch (InvalidOperationException ex)
                {
                    contar = 0;
                }

                return contar;
            }
        }

        protected void lkComentar_Click(object sender, EventArgs e)
        {
            using (var SmartVctx = new SmartVoidEntities())
            {
                Usuario user = Session["Usuario"] as Usuario;
                String idDebate = Request.QueryString["IdDebate"];
                String IniciadoPor = user.u_Usuario;
                String PathComentario = "ComentariosDebate.aspx?idDebate=" + idDebate + "";

                Comentario objComentario = new Comentario();
                objComentario.c_IDGrupo = idGrupo;
                objComentario.c_IDDebate = Convert.ToInt32(idDebate);
                objComentario.c_IDComentario = contarComentarios() + 1;
                objComentario.c_Autor = IniciadoPor;
                objComentario.c_Mensaje = txtPrimerComentario.Text;
                objComentario.c_FechaDeCreacion = DateTime.Now;

                SmartVctx.Comentarios.AddObject(objComentario);
                SmartVctx.SaveChanges();
                Response.Redirect(PathComentario);
            }
        }

    }
}